package com.android.pagemarket.ui.widget;

import java.util.Vector;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

import com.android.pagemarket.util.OnTabChangedListener;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-9-25
 * @see : 通用水平滑动tab索引视图
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class TabIndexView implements OnClickListener
{
    private Vector<TabView> mTabIndexViews = new Vector<TabView>();
    private OnTabChangedListener mOnTabListener;
    private int mCurTabId = -1;
    
    public TabIndexView()
    {
        
    }
    
    public void setOnTabChangedListener(OnTabChangedListener listener)
    {
        mOnTabListener = listener;
    }
    
    public void addTab(int id, View tabView, Intent intent)
    {
        mTabIndexViews.add(new TabView(id,tabView,intent));
        tabView.setOnClickListener(this);
    }

    public void showTab(int id)
    {
        if(mCurTabId != id)
        {
            for(TabView one : mTabIndexViews)
            {
                if(one.id == id)
                {
                    onClick(one.view);
                }
            }
        }
    }
    
    public void setTabVisible(int id, boolean visible)
    {
        for(TabView one : mTabIndexViews)
        {
            if(one.id == id)
            {
                one.view.setVisibility(visible ? View.VISIBLE : View.GONE);
            }
        }
    }

    @Override
    public void onClick(View v)
    {
        for(TabView one : mTabIndexViews)
        {
            if(one.view != v)
            {
                one.view.setSelected(false);
            }else
            {
                v.setSelected(true);
                mCurTabId = one.id;
                if(mOnTabListener != null)
                {
                    mOnTabListener.onTabChanged(one.id, one.intent);
                }
            }
        }
    }
    
    public class TabView
    {
        int id;
        View view;
        Intent intent;
        
        public TabView(int id,View view,Intent intent)
        {
            this.id = id;
            this.view = view;
            this.intent = intent;
        }
    }
}
